public void surfaceCreated(SurfaceHolder holder) {
//open the camera
try {
camera = Camera.open();
} catch (RuntimeException ex) {
}
Camera.Parameters parameters;
parameters = camera.getParameters();
//modify parameter
parameters.setPreviewFrameRate(20);
parameters.setPreviewSize(352, 288);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
try {
//The surface thas been created, now tell the camera where to draw
//the preview
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
refreshCamera();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//stop preview and release camera
camera.stopPreview();
camera.release();
camera = null;
}
}
In these lines, we find our SurfaceView from our main layout and we get a holder, an abstract interface to someone holding a display surface. This, allows us to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface. We also install a SurfaceHolder.Callback so we get notified when the underlying surface is created and destroyed. We have also written the surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); deprecated setting, but required on Android versions prior to 3.0.
XML stands for Extensible Mark-up Language.XML is a very popular format and commonly used for sharing data on the internet. This chapter explains how to parse the XML file and extract necessary information from it.
XML-Parsing
we will create XMLPullParser object , but in order to create that we will first create XmlPullParserFactory object and then call its newPullParser() method to create XMLPullParser
Example:
Here is an example demonstrating the use of XMLPullParser class. It creates a basic Weather application that allows you to parse XML from google weather api and show the result.
Following is the content of the modified main activity file MainActivity.java.
package androiddemo.example.duyhoang.xmlparsedemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText txt1, txt2, txt3, txt4, txt5;
private String url1 = "http://api.openweathermap.org/data/2.5/weather?q=";
private String url2 = "&mode=xml";
private HandleXml obj;
Button btnWeather;
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnWeather = (Button) findViewById(R.id.btn_weather);
txt1 = (EditText) findViewById(R.id.txtLocation);
txt2 = (EditText) findViewById(R.id.txt_curency);
txt3 = (EditText) findViewById(R.id.txt_temp);
txt4 = (EditText) findViewById(R.id.txt_humidity);
txt5 = (EditText) findViewById(R.id.txt_pressure);
btnWeather.setOnClickListener(new View.OnClickListener() {
@Overridepublic void onClick(View view) {
String url = txt1.getText().toString();
String finalUrl = url1 + url + url2;
txt2.setText(finalUrl);
obj = new HandleXml(finalUrl);
obj.fetchXML();
while (obj.parsingComplete) ;
txt2.setText(obj.getCountry());
txt3.setText(obj.getTemperature());
txt4.setText(obj.getHumidity());
txt5.setText(obj.getPressure());
}
});
}
@Overridepublic boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Overridepublic boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();
//noinspection SimplifiableIfStatementif (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Following is the content of src/com.example.xmlparser/HandleXML.java.
package androiddemo.example.duyhoang.xmlparsedemo;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/** * Created by DUYHOANG on 9/10/2015. */public class HandleXml {
private String country = "country";
private String temperature = "temperature";
private String humidity = "humidity";
private String pressure = "pressure";
private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;
public volatile boolean parsingComplete = true;
public HandleXml(String url) {
this.urlString = url;
}
public String getCountry() {
return country;
}
public String getTemperature() {
return temperature;
}
public String getHumidity() {
return humidity;
}
public String getPressure() {
return pressure;
}
public void parseXMLAndStorelt(XmlPullParser myParser) {
int event;
String text = null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name = myParser.getName();
switch (event) {
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if (name.equals("country")) {
country = text;
} else if (name.equals("humidity")) {
humidity = myParser.getAttributeValue(null, "value");
} else if (name.equals("pressure")) {
pressure = myParser.getAttributeValue(null, "value");
} else if (name.equals("temperature")) {
temperature = myParser.getAttributeValue(null, "value");
} else {
}
break;
}
event = myParser.next();
}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void fetchXML() {
Thread thread = new Thread(new Runnable() {
@Overridepublic void run() {
try {
URL url = new URL(urlString);
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
connect.setReadTimeout(10000);
connect.setConnectTimeout(15000);
connect.setRequestMethod("GET");
connect.setDoInput(true);
connect.connect();
InputStream stream = connect.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
myparser.setInput(stream, null);
parseXMLAndStorelt(myparser);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
The onCreateDialog() method is a callback for creating dialogs that are managed by the activity. When you call the showDialog() method, this callback will be invoked. The showDialog() method accepts an integer argument identifying a particular dialog to display.